package com.xmtlxx.tms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.tms.domain.Car;
import com.xmtlxx.tms.domain.bo.CarBo;
import com.xmtlxx.tms.domain.vo.CarVo;
import com.xmtlxx.tms.mapper.CarMapper;
import com.xmtlxx.tms.mapper.CarrierMapper;
import com.xmtlxx.tms.service.ICarService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmtlxx.system.domain.SysOss;
import com.xmtlxx.system.mapper.SysDeptMapper;
import com.xmtlxx.system.mapper.SysOssMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 车辆Service业务层处理
 *
 * @author xmtlxx
 * @date 2022-08-15
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class CarServiceImpl implements ICarService {

    private final CarMapper baseMapper;

    private final SysOssMapper ossMapper;

    private final CarrierMapper carrierMapper;

    private final SysDeptMapper deptMapper;

    /**
     * 查询车辆
     *
     * @param id 车辆主键
     * @return 车辆
     */
    @Override
    public CarVo queryById(String id){
        //return baseMapper.selectVoById(id);
        return baseMapper.selectCarVoByID(id);
    }

    /**
     * 查询车辆列表
     *
     * @param bo 车辆
     * @return 车辆
     */
    @Override
    public TableDataInfo<CarVo> queryPageList(CarBo bo, PageQuery pageQuery) {
       // LambdaQueryWrapper<Car> lqw = buildQueryWrapper(bo);
       // Page<CarVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<CarVo> result = baseMapper.selectPageCarList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询车辆列表
     *
     * @param bo 车辆
     * @return 车辆
     */
    @Override
    public List<CarVo> queryList(CarBo bo) {
       // LambdaQueryWrapper<Car> lqw = buildQueryWrapper(bo);
       // return baseMapper.selectVoList(lqw);
        return baseMapper.selectCarList(bo);
    }

    private LambdaQueryWrapper<Car> buildQueryWrapper(CarBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Car> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCarrierId()), Car::getCarrierId, bo.getCarrierId());
        lqw.eq(StringUtils.isNotBlank(bo.getCarNumber()), Car::getCarNumber, bo.getCarNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getCarType()), Car::getCarType, bo.getCarType());
        lqw.eq(StringUtils.isNotBlank(bo.getPlateColor()), Car::getPlateColor, bo.getPlateColor());
        lqw.eq(StringUtils.isNotBlank(bo.getCooperation()), Car::getCooperation, bo.getCooperation());
        lqw.like(StringUtils.isNotBlank(bo.getOwnerName()), Car::getOwnerName, bo.getOwnerName());
        lqw.like(StringUtils.isNotBlank(bo.getVin()), Car::getVin, bo.getVin());
        lqw.eq(StringUtils.isNotBlank(bo.getEngineNo()), Car::getEngineNo, bo.getEngineNo());
        lqw.between(params.get("beginGrantDate") != null && params.get("endGrantDate") != null,
            Car::getGrantDate ,params.get("beginGrantDate"), params.get("endGrantDate"));
        lqw.eq(StringUtils.isNotBlank(bo.getEnergyType()), Car::getEnergyType, bo.getEnergyType());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), Car::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), Car::getDelFlag, bo.getDelFlag());
        lqw.eq(bo.getDeptId() != null, Car::getDeptId, bo.getDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getUseCharacter()), Car::getUseCharacter, bo.getUseCharacter());
        lqw.between(params.get("beginRegisterDate") != null && params.get("endRegisterDate") != null,
            Car::getRegisterDate ,params.get("beginRegisterDate"), params.get("endRegisterDate"));
        lqw.eq(StringUtils.isNotBlank(bo.getFileNo()), Car::getFileNo, bo.getFileNo());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), Car::getCreateBy, bo.getCreateBy());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), Car::getUpdateBy, bo.getUpdateBy());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            Car::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
        return lqw;
    }

    /**
     * 新增车辆
     *
     * @param bo 车辆
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(CarBo bo) {
        Car add = BeanUtil.toBean(bo, Car.class);
        validEntityBeforeSave(add);

        //填充UUID
        add.setId(IdUtil.simpleUUID());
        add.setDelFlag("0");

        add.setDeptId(carrierMapper.selectById(add.getCarrierId()).getDeptId());
        add.setDeptName(carrierMapper.selectById(add.getCarrierId()).getDeptName());

        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        if(bo.getOss() != null && bo.getOss().size() > 0){
            for (SysOss oss:bo.getOss()){
                SysOss sysOss = new SysOss();
                BeanUtil.copyProperties(oss,sysOss);
                sysOss.setObjid(add.getId());
                sysOss.setBuss("car");
                ossMapper.insertOrUpdate(sysOss);
            }
        }

        return flag;
    }

    /**
     * 修改车辆
     *
     * @param bo 车辆
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(CarBo bo) {
        Car update = BeanUtil.toBean(bo, Car.class);
        validEntityBeforeSave(update);
        update.setDeptId(carrierMapper.selectById(update.getCarrierId()).getDeptId());
        update.setDeptName(carrierMapper.selectById(update.getCarrierId()).getDeptName());
        if(bo.getOss() != null && bo.getOss().size() > 0){
            for (SysOss oss:bo.getOss()){
                SysOss sysOss = new SysOss();
                BeanUtil.copyProperties(oss,sysOss);
                sysOss.setObjid(update .getId());
                sysOss.setBuss("car");
                ossMapper.insertOrUpdate(sysOss);
            }
        }

        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(Car entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除车辆
     *
     * @param ids 需要删除的车辆主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
